﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.Control
{
    public partial class DKChung_DkKho : UserControl
    {
        public DKChung_DkKho()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            gridKho.DataSource = DataAll.SelectAll("DkChung_Kho", "*", "");
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            //gvNhomHang.BestFitColumns();
            gvKho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvKho.OptionsBehavior.Editable = false;
            gvKho.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvKho.OptionsSelection.MultiSelect = true;
            gvKho.OptionsView.ShowGroupPanel = false;
            gvKho.OptionsView.ColumnAutoWidth = true;
            gvKho.OptionsView.ShowFooter = true;
            gvKho.OptionsFind.AlwaysVisible = true;
            gvKho.Columns["IDKho"].Visible = false;
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            var dkkho = new DKChung_DkKhoPopup();
            dkkho.ShowDialog();
            LoadData();
        }

        private void gvKho_DoubleClick(object sender, EventArgs e)
        {
            if (gvKho.FocusedRowHandle >= 0)
            {
                DKChung_DkKhoPopup.MaID = gvKho.Columns["IDKho"].View.GetRowCellDisplayText(gvKho.FocusedRowHandle, "IDKho");
                var dkkho = new DKChung_DkKhoPopup();
                dkkho.update = true;
                dkkho.ShowDialog();
                LoadData();
            }
        }

        private void btnXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvKho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa kho này ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Delete("DkChung_Kho", "IDKho = " + dataview[gvKho.FocusedRowHandle]["IDKho"]);
                MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            LoadData();
        }
    }
}
